package Java.view.game.douniu;

import domain.Player;
import domain.User;

import java.awt.*;
import java.io.BufferedWriter;
import java.io.IOException;
import java.net.Socket;
import java.util.*;

import static javafx.application.Application.launch;

public class douniuRoom {
    ArrayList<Socket> array;
    static int count=0;
    static ArrayList<Player> playerArray;
    public static User[] users=new User[3];

    int start;
    static private BufferedWriter dos1;
    static private BufferedWriter dos2;
    static private BufferedWriter dos3;

    //三个人的牌
    static ArrayList<String> player1Card = new ArrayList<>();
    static ArrayList<String> player2Card = new ArrayList<>();
    static ArrayList<String> player3Card = new ArrayList<>();
    //三人是否停牌
    public static int playerStop = 0;
    static ArrayList<String> list = new ArrayList<>();//代表一副牌

    public static String p1 = null;
    public static String p2 = null;
    public static String p3 = null;

    static {
        //准备牌
        //运用静态代码块，随着类的加载而加载，而且只执行一次
        //牌库
        for (int i = 0; i <= 51; i++) {
            String name = String.valueOf(i);
            list.add(name);
        }
    }

    public douniuRoom(){}

    //创建房间时，将三个玩家加进来
    public void addPlayer(ArrayList<Player> players){
        playerArray=players;
        if(playerArray.size()==3){
            dos1=playerArray.get(0).writer;
            dos2=playerArray.get(1).writer;
            dos3=playerArray.get(2).writer;
        }

    }
    //发牌给房间中的三位用户
    public static void distributePoker(Player player) throws IOException {
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (playerArray.size() == 3&&count==0) {
                    try {
                        count++;
                        sendInfo(player);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Date(), 1500);
    }
    //发送名字和5张牌
    private static void sendInfo(Player player) throws IOException {
        //发玩家名
        dos1.write(playerArray.get(0).getUserName()+"#"+playerArray.get(1).getUserName()+"#"+playerArray.get(2).getUserName());
        dos1.newLine();
        dos1.flush();
        dos2.write(playerArray.get(0).getUserName()+"#"+playerArray.get(1).getUserName()+"#"+playerArray.get(2).getUserName());
        dos2.newLine();
        dos2.flush();
        dos3.write(playerArray.get(0).getUserName()+"#"+playerArray.get(1).getUserName()+"#"+playerArray.get(2).getUserName());
        dos3.newLine();
        dos3.flush();
        //洗牌
        Collections.shuffle(list);
        //发牌
        //遍历牌库得到每一张牌
        for (int i = 0; i < 15; i++) {
            getPoker(i);
        }
        //distribute#[card]$
        //startGame#索引#牌$
        //发玩家123的前四张牌
        String p1Poker="startGame#"+"1#"+player1Card.toString().replaceAll("\\[|\\]", "").replaceAll(", ", " ") + "$";
        String p2Poker="startGame#"+"2#"+player2Card.toString().replaceAll("\\[|\\]", "").replaceAll(", ", " ") + "$";
        String p3Poker="startGame#"+"3#"+player3Card.toString().replaceAll("\\[|\\]", "").replaceAll(", ", " ") + "$";

        dos1.write(p1Poker+p2Poker+p3Poker);
        dos1.newLine();
        dos1.flush();

        dos2.write(p2Poker+p3Poker+p1Poker);
        dos2.newLine();
        dos2.flush();

        dos3.write(p3Poker+p1Poker+p2Poker);
        dos3.newLine();
        dos3.flush();
    }
    //发牌
    private static void getPoker(int i) {
        String poker = list.get(i);
        if (i % 3 == 0) {
            player1Card.add(poker); //玩家1牌
        } else if (i % 3 == 1) {
            player2Card.add(poker); //玩家2牌
        } else {
            player3Card.add(poker); //玩家3牌
        }
    }
    //发送加注金额
    public static void addBet(int happyDou) throws IOException {
        System.out.println("addBet");
        dos1.write("addBet#"+ happyDou +"#$");
        dos1.newLine();
        dos1.flush();
        dos2.write("addBet#"+ happyDou +"#$");
        dos2.newLine();
        dos2.flush();
        dos3.write("addBet#"+ happyDou +"#$");
        dos3.newLine();
        dos3.flush();
    }
    //发送玩家停注状态
    public static void stopBet(int index) throws IOException {
        System.out.println("stopBet");
        playerStop++;
        //显示第五张牌
        if(playerStop == 3){
            System.out.println("playerStop"+playerStop);
            dos1.write("stopBet#"+index+"#$");
            dos1.newLine();
            dos1.flush();
            dos2.write("stopBet#"+index+"#$");
            dos2.newLine();
            dos2.flush();
            dos3.write("stopBet#"+index+"#$");
            dos3.newLine();
            dos3.flush();
            playerStop = 0; //重置
        }
    }
    //获取分数
    public static void getScore(int i, int j) throws IOException {
        System.out.println("getScore");

        if(i == 1){
            p1= "#1#" + j + "#2#";
        }
        else if(i == 2){
            p2=+j + "#3#";
        }
        else if(i == 3){
            p3=j + "#";
        }
        System.out.println(p1+p2+p3);
        if(p1!=null && p2!=null && p3!=null)
        {
            dos1.write("score#1" +p1 + p2 + p3 );
            dos1.newLine();
            dos1.flush();

            dos2.write("score#2" + p1 + p2 + p3);
            dos2.newLine();
            dos2.flush();

            dos3.write("score#3" +p1 + p2 + p3);
            dos3.newLine();
            dos3.flush();
        }else {
            System.out.println("还没都确定");
        }
    }

}
